<?php
/**
 * User: Administrator
 * Note: 系统设置
 */
namespace Admin\Controller;

class ConfigController extends BaseController
{
    // 系统配置
    public function index()
    {
        if (IS_POST) {
            $post = I('post.', '', 'htmlspecialchars');
            $res  = '';
            foreach ($post as $key => $item) {
                $have = M('config')->where(array('name'=>$key))->find();
                if (!$have) {
                    appReturn(0, '参数：'.$key.'不存在');
                    die();
                }
                $result = M('config')->where(array('name'=>$key))->save(array('value'=>$item));
                if ($result === false) {
                    $res .= ','.$key;
                }
            }
            if ($res) {
                appReturn(0, '保存失败', $res);
            } else {
                appReturn(1, '保存成功');
            }
        } else {
            $config  = M('config')->where(array('type'=>1))->select();//系统配置
            $smsinfo = M('config')->where(array('type'=>2))->select();//短信配置
            $this->assign("config", $config);
            $this->assign("smsinfo", $smsinfo);
            $this->assign('meta_title', '系统配置');
            $this->display();
        }
    }
    // 保存设置
    public function savesms()
    {
        if (IS_POST) {
            $post = I('post.', '', 'htmlspecialchars');
            // print_r($post);die();
            $res  = '';

            foreach ($post as $key => $item) {
                $have = M('config')->where(array('name'=>$key))->find();
                if (!$have) {
                    appReturn(0, '参数：'.$key.'不存在');
                    die();
                }
                $result = M('config')->where(array('name'=>$key))->save(array('value'=>$item));
                if ($result === false) {
                    $res .= ','.$key;
                }
            }
            if ($res) {
                appReturn(0, '保存失败', $res);
            } else {
                appReturn(1, '保存成功');
            }
        }
    }
    // 修改密码
    public function modadmin()
    {
        if (IS_POST) {
            $post = I('post.');

            $info = M('member')->where(array('id'=>$post['id']))->find();
            if (passwordMd5($post['oldpassword']) != $info['password']) {
                appReturn(0, '原密码输入有误');
            }

            if ($post['password'] !== $post['repassword']) {
                appReturn(0, '新密码两次输入不一样');
            }

            $data = array('password'=>passwordMd5($post['password']));
            if (strlen($post['password']) < 8) {
                appReturn(0, '密码至少8位');
            }
            if ($post['password'] == passwordMd5('12345678')) {
                appReturn(0, '密码不得与初始密码相同');
            }
            if (M('member')->where(array('id'=>$post['id']))->save($data)) {
                session('admin_changePassword', null);
                appReturn(0, '保存成功');
            }
            appReturn(0, '保存失败');
        } else {
            $admin = M('member')->where(array('id'=>session('admin_uid')))->find();
            $this->assign("admin", $admin);
            $this->assign('meta_title', '管理员信息修改');
            $this->display();
        }
    }
    //清理缓存
    public function delCache()
    {
        $admin_dir = "./Application/Admin/Runtime/Cache/Admin";
        $home_dir  = "./Application/Home/Runtime/Cache/Home";
        $res       = del_dir($admin_dir);
        $result    = del_dir($home_dir);
        if (IS_AJAX && $res && $result) {
            appReturn(1, '清除成功');
        } else {
            redirect(U('Index/index'));
        }
    }
}
